package cn.psh.basic.Interceptor;

import cn.psh.basic.jwt.JwtUtils;
import cn.psh.basic.jwt.LoginData;
import cn.psh.basic.jwt.Payload;
import cn.psh.basic.jwt.RsaUtils;
import cn.psh.basic.util.annotation.PreAuthorize;
import cn.psh.org.mapper.EmployeeMapper;
import cn.psh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PublicKey;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception {
        //1.获取token
        String token = request.getHeader("token");
        //3.如果有token，通过token获取redis的登录信息
        if(token != null){
//      获取公钥解密
            Logininfo info=null;
            try {
                final PublicKey publicKey = RsaUtils.getPublicKey(LoginInterceptor.class.getClassLoader().getResource("auth_rsa.pub").getFile());
//            解密之后获取载荷
                final Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                info = payload.getLoginData().getLogininfo();
            } catch (Exception e) {
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().println("{\"success\":false,\"message\":\"timeOut\"}");
                return false;
            }
//                如果你是用户直接放行
                if(info.getType()==1){
                    return true;
                }
//                获取登陆人当前的所有权限
                List<String> sns = employeeMapper.queryAllPermission(info.getId());
                System.out.println(sns+"----------------------------------------------");
//                获取打了@preAuthorize
                HandlerMethod handlerMethod =  (HandlerMethod)handler;
                PreAuthorize preAuthorize = handlerMethod.getMethodAnnotation(PreAuthorize.class);
                if(preAuthorize == null){
                    return true;

                }
                String sn = preAuthorize.sn();
                if(sns.contains(sn)){
                    return true;
                }
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().println("{\"success\":false,\"message\":\"noPermission\"}");
                return false;
        }
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println("{\"success\":false,\"message\":\"noLogin\"}");
        return false;

    }
}
